package com.sicau.smarttraceplatform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sicau.smarttraceplatform.entity.Trace;
import com.sicau.smarttraceplatform.entity.param.trace.TraceQueryH5Param;
import com.sicau.smarttraceplatform.entity.param.trace.TraceQueryParam;
import com.sicau.smarttraceplatform.entity.vo.PageDataVO;
import com.sicau.smarttraceplatform.entity.vo.trace.TraceInfoVO;
import com.sicau.smarttraceplatform.mapper.TraceMapper;
import com.sicau.smarttraceplatform.mapper.vo.TraceInfoVOMapper;
import com.sicau.smarttraceplatform.service.TraceService;
import com.sicau.smarttraceplatform.utils.PageUtil;
import com.sicau.smarttraceplatform.utils.QueryWrapperUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * <p>
 * 耕种管收数据 服务实现类
 * </p>
 *
 * @author sxt && ttxxi
 * @since 2020-05-24
 */
@Service
public class TraceServiceImpl extends ServiceImpl<TraceMapper, Trace> implements TraceService {

    @Resource
    private TraceInfoVOMapper traceInfoVOMapper;

    /**
     * 排序属性
     */
    private static final String[] ORDER_COLUMN = {"created"};

    @Override
    public PageDataVO<TraceInfoVO> queryTraceInfoByCondition(TraceQueryParam queryParam) {
        QueryWrapper<TraceInfoVO> queryWrapper = QueryWrapperUtil.getQueryWrapper(TraceInfoVO.class, TraceQueryParam.class, queryParam);
        queryWrapper.orderByDesc(ORDER_COLUMN);
        Page<TraceInfoVO> infoVOPage = new Page<>(queryParam.getCurrentPage(), queryParam.getPageSize());
        infoVOPage = traceInfoVOMapper.selectPage(infoVOPage, queryWrapper);
        return PageUtil.convert(infoVOPage);
    }

    @Override
    public PageDataVO<TraceInfoVO> queryTraceInfoH5ByCondition(TraceQueryH5Param traceQueryH5Param) {
        LambdaQueryWrapper<TraceInfoVO> queryWrapper = new LambdaQueryWrapper<TraceInfoVO>()
                .eq(!StringUtils.isEmpty(traceQueryH5Param.getCategoryId()),TraceInfoVO::getCategoryId,traceQueryH5Param.getCategoryId())
                .eq(!StringUtils.isEmpty(traceQueryH5Param.getUserId()),TraceInfoVO::getUserId,traceQueryH5Param.getUserId())
                .in(traceQueryH5Param.getAreaIdList()!=null && !traceQueryH5Param.getAreaIdList().isEmpty() ,TraceInfoVO::getAreaId,traceQueryH5Param.getAreaIdList())
                .orderByAsc(TraceInfoVO::getId);
        Page<TraceInfoVO> infoVOPage = new Page<>(traceQueryH5Param.getCurrentPage(), traceQueryH5Param.getPageSize());
        infoVOPage = traceInfoVOMapper.selectPage(infoVOPage, queryWrapper);
        return PageUtil.convert(infoVOPage);
    }
}
